// fetches the given new items into the new items table 
function fetchNewItems(newItemsNumber,items){
	if(currentPage != Pages.HOME || newItemsNumber < 1)
		return;
	var even = true;
	for(var i=0;i<newItemsNumber;i++){
		var tableRow = document.createElement('tr');
		if(even)
			tableRow.setAttribute('class', 'alt');
		even = !even;
		var thumbnail = document.createElement('img');
		thumbnail.setAttribute('src', items[i].pictureURL);
		thumbnail.setAttribute('onclick', 'viewItem(' + i + ')');
		var tableData = document.createElement('td');
		tableData.appendChild(thumbnail);
		tableData.innerHTML +=  "&nbsp;&nbsp;&nbsp;" + items[i].name + ":" + items[i].description ;
		tableRow.appendChild(tableData);
		var container = document.getElementById('newitemtsablebody');
		container.insertBefore(tableRow, container.firstChild);
		if(ifTableFull())
		container.removeChild(container.lastChild);
		//so that we will keep only 4 elements in the same table
	}
}

function fetchItemsIntoTable(items,tableID,onClickFunc,isTableRowsLimited){
	for(var i=0;i<newItemsNumber;i++){
		var table = document.getElementById(tableID);
		var row = table.insertRow(0);
		if(even = !even)
			row.setAttribute('class', 'alt');
		var cell1 = row.insertCell(0);
		var thumbnail = document.createElement('img');
		thumbnail.setAttribute('src', items[i].pictureURL);
		thumbnail.setAttribute('onclick', onClickFunc + '(' + i + ')'); //TODO:
		cell1.appendChild(thumbnail);
		cell1.innerHTML +=  "&nbsp;&nbsp;&nbsp;" + items[i].name + ":" + items[i].description ;
		row.insertCell(1);
		row.insertCell(2);
		if(isTableRowsLimited && ifTableOverFlow()){
			container.removeChild(container.lastChild);
		}
		//so that we will keep only 4 elements in the same table
	}
}


function ifTableOverFlow(table){
	return (table.rows.length > 4) ? true : false;
}
//fetch searched items
function fetchSearchedItems(items,tableID,onClickFunc,isTableRowsLimited){
	var even = true;
	for(var i = 0;i < items.lentgh; i++){
		var tableRow = document.createElement('tr');
		if(even)
			tableRow.setAttribute('class', 'alt');
		var thumbnail = document.createElement('img');
		thumbnail.setAttribute('src', items[i].pictureURL);
		thumbnail.setAttribute('onclick', onClickFunc + '(' + i + ')');
		var tableData1 = document.createElement('td');
		tableData1.appendChild(thumbnail);
		tableData1.innerHTML +=  "&nbsp;&nbsp;&nbsp;" + items[i].name + ":" + items[i].description ;
		tableRow.appendChild(tableData1);
		var tableData2 = document.createElement('td');
		tableRow.appendChild(tableData2);
		var tableData3 = document.createElement('td');
		tableRow.appendChild(tableData3);
		var container = document.getElementById('tableID');
		container.insertBefore(tableRow, container.firstChild);
		if(isTableRowsLimited && ifTableFull()){
			container.removeChild(container.lastChild);
		}
	}
	//to update the current bid amount and time left
	updateItemsDetailsArray();
}
//posting a new item 

function postItem(){
	var params = new Object();
	params.name = document.getElementById("productname").value;
	params.description = document.getElementById("productdescription").value;
	params.pictureURL = document.getElementById("pictureURL").value;
	params.currentBitAmount = document.getElementById("initialBitAmount").value;
	params.selltimout = document.getElementById("selltimoutSeconds").value * 1000;
	params.selltimout += document.getElementById("selltimoutMinutes").value * 1000 * 60;
	params.selltimout += document.getElementById("selltimoutHours").value * 1000 * 60 * 60 *24;
	params.selltimout += document.getElementById("selltimoutDays").value * 1000 * 60 * 60 *24;
	newItem(params);
}

function validRange(number,min,max,int){
	return (number > min && number < max && (!int || number % 1 == 0));
}

function checkValidTime(){
	var timeFields = ["selltimoutDays","selltimoutMinutes","selltimoutHours","selltimoutSeconds"];
	for(var i = 0; i < timeFields.length; i++){
	if(!validRange(document.getElementById(timeFields[i]).value,-1,60,true)){
			showError("time input must be 0-59");
	}	
	}
}

var inf = 1.7976931348623157E+10308;

function checkValidOpeningPrice(){
	if(!(validRange(document.getElementById("initialBitAmount").value,-1,inf,false))){
		showError("opening price must be positive");
	}
}



//const string 
var dollarString = "$", left = "left",minString = "m ",secString="sec ",SOLD_FOR = "sold for ";

function getTimeString(date){
	var sec = date.getTime()/1000;
	return (sec < 60) ? sec + secString : sec/60 + minString;
}

//filing the view item page
function fillViewItemPage(item){
	document.getElementById('itemname').innerHTML = item.name;
	document.getElementById('itemdecsription').innerHTML = item.decsription;
	document.getElementById('viewitempage').setAttribute("src", item.pictureURL);
	modifyCurrentBitAmount(item.currentBitAmount);
	modifyTimeLeft(item.sellTimout);
	document.getElementById("itemid").value = itemID;
	if(!item.isMine){
	document.getElementById('money').setAttribute('hidden', 'false');
	document.getElementById('submit').setAttribute('hidden', 'false');
	}
}


//update item new details
function updateItemsDetails(){
	if(currentPage == Pages.VIEWITEM){
		updateCurrentItem();
		}else{
			if(currentPage == Pages.SEARCH){
			updateItemsDetailsArray(searchedItems,'newitemtsablebody');
			}else if(currentPage == Pages.HOME){
			updateItemsDetailsArray(newItemsArr,'newitemtsablebody');
			}
}
}

function updateItemsDetailsArray(itemsArr,tableID){
	var tabl = document.getElementById(tableID);
	var rowsLength = tabl.rows.length;
	for(var i=0;i<rowsLength;i++){
		var item = itemsArr[i];
		if(item.winnerUserName == null || item.sellTime == 0){
		tabl.rows[i].cells[1].innerText = itemsArr[i].currentBidAmount + dollarString;
		tabl.rows[i].cells[2].innerText = getTimeString(itemsArr[i].sellTime);
		}else{
			tabl.rows[i].cells[1].innerText = SOLD_FOR  + getStringWithDollarSign(item.currentBitAmount);
			tabl.rows[i].cells[2].innerText = "";
		}
	}
} 

function updateCurrentItem(){
	var currentBit = document.getElementById("currentbid");
	var timeLeft = document.getElementById("timeleft");
	if(selectedItem.winnerUserName == null || selectedItem.sellTime == 0){
		currentBit.innerHTML = getStringWithDollarSign(selectedItem.currentBidAmount);
		timeLeft.innerHTML = getTimeString(selectedItem.sellTime);	
		}else{
			currentBit.innerHTML = SOLD_FOR + getStringWithDollarSign(selectedItem.currentBitAmount);
			timeLeft.innerHTML = "";
		}
	}

function getStringWithDollarSign(string){
	return string + dollarString;
}


function viewNewItem(newItemIndex){
	switchToViewItem();
	fillViewItemPage(newItemsArr[newItemIndex]);
}

function viewSearchedItem(searchedItemIndex){
	switchToViewItem();
	fillViewItemPage(newItemsArr[searchedItemIndex]);
}